/**
 * GWT Kad : Kademlia implementation for GWT
 * Copyright (C) 2012  Pierrick HYMBERT, Paul-André MISTRAL, Florian ALEXANDRE
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.kad.gwt.web.client.kademelia.rpc.ping.mvp;

import org.kad.gwt.web.client.kademelia.model.rpc.ping.KadRpcPingRequest;
import org.kad.gwt.web.client.kademelia.model.rpc.ping.KadRpcPongResponse;

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;

/**
 * Fired when the event source receive a ping from the kad network.
 * 
 * @author Pierrick HYMBERT &lt;pierrick [dot] hymbert [at] gmail [dot] com&gt;
 */
public class KadPingEvent extends GwtEvent<KadPingEvent.Handler> {
    public interface Handler extends EventHandler {
        void onKadPing(KadPingEvent event);
    }

    /**
     * The event type.
     */
    static Type<KadPingEvent.Handler> TYPE;

    /**
     * Fires an {@link KadPingEvent} on all registered handlers in the handler source.
     * 
     * @param <S>
     *            The handler source type
     * @param source
     *            the source of the handlers
     * @param request
     *            {@link #request}
     * @param response
     *            {@link #response}
     * @return The event
     */
    public static <S extends HasKadPingHandlers> KadPingEvent fire(S source, KadRpcPingRequest request) {
        KadPingEvent event = null;
        if (TYPE != null) {
            event = new KadPingEvent(request);
            source.fireEvent(event);
        }
        return event;
    }

    /**
     * Ensures the existence of the handler hook and then returns it.
     * 
     * @return returns a handler hook
     */
    public static Type<KadPingEvent.Handler> getType() {
        if (TYPE == null) {
            TYPE = new Type<KadPingEvent.Handler>();
        }
        return TYPE;
    }

    /**
     * The associated request ping.
     */
    private final KadRpcPingRequest pingRequest;
    /**
     * The associated pong.
     */
    private KadRpcPongResponse pongResponse;

    /**
     * Construct a new {@link KadPingEvent}.
     * 
     * @param pingRequestIn
     *            {@link #pingRequest}
     */
    protected KadPingEvent(KadRpcPingRequest pingRequestIn) {
        this.pingRequest = pingRequestIn;
    }

    /**
     * @return {@link #pingRequest}
     */
    public KadRpcPingRequest getPingRequest() {
        return pingRequest;
    }

    /**
     * @return {@link #pongResponse}
     */
    public KadRpcPongResponse getPongResponse() {
        return pongResponse;
    }

    /**
     * @param pongResponse {@link #pongResponse}
     */
    public void setPongResponse(KadRpcPongResponse pongResponseIn) {
        this.pongResponse = pongResponseIn;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final Type<KadPingEvent.Handler> getAssociatedType() {
        return TYPE;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void dispatch(KadPingEvent.Handler handler) {
        handler.onKadPing(this);
    }
}
